package com.codinghero.zqlb.spider.handler;

import java.sql.SQLException;
import java.util.List;

import com.codinghero.sql.DBOper;
import com.codinghero.sql.MySQLOper;
import com.codinghero.zqlb.Constant;
import com.codinghero.zqlb.match.SoccerMatch;

@Deprecated
public class ListToTempHandler implements MatchHandler {

	public <T extends SoccerMatch> void handle(List<T> list) {
		DBOper dbOper = new MySQLOper(Constant.JDBC_URL, Constant.USER_ID, Constant.PASSWORD);
		try {
			dbOper.connect();
			dbOper.startTransaction();
			
			// clear
			dbOper.executeUpdate("TRUNCATE TABLE zqlb_soccer_match_temp");
			// insert to temp
			for (SoccerMatch match : list) {
				String sql = "INSERT INTO zqlb_soccer_match_temp "
						+ " (`typeId`, typeName, startDate, endDate, "
						+ " team1Id, team2Id, team1Name, team2Name, " +
						" typeFocused, team1Focused, team2Focused, " +
						" channelIds, channelNames, insertTime) " + "VALUES ("
						+ match.getTypeId() + ", '" + match.getTypeName() + "', '"
						+ match.getStartDateStr() + "', NULL, "
						+ match.getTeam1Id() + ", " + match.getTeam2Id() + ", '"
						+ match.getTeam1Name() + "', '" + match.getTeam2Name() + "', " 
						+ match.getTypeFocused() + ", " + match.getTeam1Focused() + ", " + match.getTeam2Focused() + ", '"
						+ match.getChannelIds() + "', '" + match.getChannelNames() + "', NOW())";
				dbOper.addBatch(sql);
			}
			
			dbOper.executeBatch();
			dbOper.commit();
		} catch (SQLException e) {
			dbOper.rollback();
			e.printStackTrace();
		} finally {
			dbOper.close();
		}
	}

}
